<?php
session_start();
 require_once "../basicInfo/user.php";
 define ("MAX_SIZE","100");
?>

<title>Debugger MMO</title>
<?php
  if( (isset($_POST['upload'])) || (isset($_POST['yes'])) )
  {
     if( isset($_POST['upload']))
     {
        $mess = "upload";
        //write the code for saving the file to db and profile page
        //below code is for uploading the file
        $target_path = "../../upload/";
        $imageName = basename( $_FILES['file']['name']);
        $target_path = $target_path . basename( $_FILES['file']['name']);
        echo($target_path);
       /*
        print $target_path;
        echo "<pre>";
        print_r($_FILES);
        echo "</pre>";
        if(($_FILES['file']['tmp_name']))
        {
            print "it is set \n ";
            print $_FILES['file']['tmp_name'];
        }
        else
        {
            print "\nits not set";
        }
        */
        
        if(filesize($_FILES["file"]["tmp_name"]) < MAX_SIZE*1024)
        {
            if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path))
            {
                //echo "The file ".  basename( $_FILES['file']['name']). " has been uploaded";

                if(isset($_SESSION['userinfo']))
                {
                    $serializedUser = $_SESSION['userinfo'];
                    $unserializedUser = unserialize($serializedUser);
                    $unserializedUser->setAvatar($imageName);
                    $uname = $unserializedUser->getUserName();
                    UserController::update_image($uname ,$imageName );
                    $serializedUser = serialize($unserializedUser);
                    $_SESSION['userinfo'] = $serializedUser;
                }
                print '<script type="text/javascript">';
                print 'opener.location.reload();';
                print 'window.close()';
                print '</script>';
            }
            else
            {
                echo "There was an error uploading the file, please try again!";
            }
        }
        else
        {
            print "File has exceeded the size limit and so cannot be uploaded";
        }
     }
     else
     {
        $mess = "remove";

        //write the code to remove file from db and redirect it to  profile page
         if(isset($_SESSION['userinfo']))
            {
                $serializedUser = $_SESSION['userinfo'];
                $unserializedUser = unserialize($serializedUser);
                $unserializedUser->setAvatar($imageName);
                $uname = $unserializedUser->getUserName();
                $imageName = NULL;
                 UserController::update_image($uname ,$imageName );
                $serializedUser = serialize($unserializedUser);
                $_SESSION['userinfo'] = $serializedUser;
            }

            print '<script type="text/javascript">';
            print 'opener.location.reload();';
            print 'window.close()';
            print '</script>';

     }
     //print "<h1>$mess</h1>";
      echo '<script language="javascript">location.replace("profile.php");</script>';
  }
  else
  {
    //call this part if no submit has been pressed
    if(!empty ($_SERVER['QUERY_STRING']))
        {

            $actionTypeArr = explode("=" ,$_SERVER['QUERY_STRING'] );
            $actionType = $actionTypeArr[1];
        }
    if($actionType == "change")
    { ?>
<html><head/>
    <body style="background-color: #FFEBCD;" >
            <!--showing the file upload dialog box -->
            <form  enctype="multipart/form-data" action="fileUpload.php" method="POST">
            <!--<input type="hidden" name="MAX_FILE_SIZE" value="10000000" /> -->
            Choose a file to upload: <input name="file" type="file" /><br />
            <input  type="submit" name="upload" value="Upload File" />
            </form>
    </body>

    <?php 
   
    }
    elseif($actionType == "remove")
    { ?>
<html><head/>
    <body style="background-color: #FFEBCD;" >
         <form  action="fileUpload.php" method="POST">
          Do you want to remove the photo? Are you sure?
          <br/><br/>
          <input  type="submit" name="yes" value="Yes"/>
          <input  type="button" value="No" onclick= "window.close();"/>
         </form>
    </body>
    <?php }
  }
?>